//
//  DMNicknameVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/17.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMNicknameVC.h"

#import "KUserControlRequest.h"

@interface DMNicknameVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *mainTF;


@end

@implementation DMNicknameVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
    
}

-(void)startInit {
    
    self.navigationItem.title = @"修改昵称";
    self.view.backgroundColor = DMDefaultColor;
    _mainTF.delegate = self;
    // 提交按钮
    UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    pushBtn.frame = CGRectMake(0, 0, 35, 40);;
    [pushBtn setTitle:@"保存" forState:UIControlStateNormal];
    [pushBtn setTitleColor:DMWhiteColor forState:UIControlStateNormal];
    pushBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:pushBtn];
    [pushBtn addTarget:self action:@selector(changeNameAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:rightBtnItem];
    [self.mainTF becomeFirstResponder];


}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
        [IQKeyboardManager sharedManager].enable = YES;
}


#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length == 10 && string.length > 0) {
        return NO;
    }
    return YES;
}

-(void)changeNameAction{
    [self.view endEditing:YES];
    KUserControlRequest * request = [[KUserControlRequest alloc]initChangeUserInfoWithNickName:self.mainTF.text profile:nil sex:nil];
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        [JLProgressHUDManager showSuccessHud:@"修改成功"];
            [KuserInfo shareInstance].userModel.nickName = weakSelf.mainTF.text;
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(KBaseModel *errorModel) {
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
}

@end
